package controllers

import (
	comm "com.wangzhumo.lottery/common"
	"com.wangzhumo.lottery/models"
)


// InsertToBlack 把用户+ip添加到黑名单
func (c *IndexController) InsertToBlack(
	clientIp string,
	loginUser *models.ObjLoginUser,
	blackUser *models.UserBlack,
	blackIp *models.IpBlack)  {

	newTime := comm.NowUnix()
	blackTime := 30 * 86400

	// update userBlack
	if blackUser == nil || blackUser.Id <= 0 {
		ub := &models.UserBlack{
			Id:         loginUser.UID,
			Username:   loginUser.Username,
			Blacktime:  newTime + blackTime,
			SysCreated: newTime,
			SysIp:      clientIp,
		}
		c.ServiceUser.Insert(ub)
	}else{
		ub := &models.UserBlack{
			Id:         loginUser.UID,
			Blacktime:  newTime + blackTime,
			SysUpdated: newTime,
		}
		c.ServiceUser.Update(ub,nil)
	}

	// update ipBlack
	if blackIp == nil || blackIp.Id <= 0 {
		ub := &models.IpBlack{
			Ip:         clientIp,
			Blacktime:   newTime + blackTime,
			SysCreated: newTime,
		}
		c.ServiceIp.Insert(ub)
	}else{
		ub := &models.IpBlack{
			Ip:         clientIp,
			Blacktime:   newTime + blackTime,
			SysUpdated: newTime,
		}
		blackIp.SysUpdated = newTime
		blackIp.Blacktime = newTime + blackTime
		c.ServiceIp.Update(ub,nil)
	}
}